package com.jams.library.commonAdapter;

/**
 * Created by DWCloud on 2016/4/27.
 */

import android.content.Context;
import android.net.Uri;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.support.v4.content.ContextCompat;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.facebook.drawee.view.SimpleDraweeView;

//import com.facebook.drawee.view.SimpleDraweeView;

/**
 * Created by jams on 2016/3/3.
 * Email 965939858@qq.com
 */
public class ListCommonViewHolder {
    private final SparseArray<View> mViews;
    private View mConvertView;
    private OnItemClickListener itemClickListener;
    int position;

    private ListCommonViewHolder(Context context, ViewGroup parent, int layoutId,
                                 int position) {
        this.mViews = new SparseArray<View>();
        mConvertView = LayoutInflater.from(context).inflate(layoutId, parent,
                false);
        this.position = position;
        mConvertView.setTag(this);
    }

    public static ListCommonViewHolder get(Context context, View convertView,
                                           ViewGroup parent, int layoutId, int position) {

        if (convertView == null) {
            return new ListCommonViewHolder(context, parent, layoutId, position);
        }

        return (ListCommonViewHolder) convertView.getTag();
    }

    public <T extends View> T getView(int viewId) {

        View view = mViews.get(viewId);
        if (view == null) {
            view = mConvertView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }

    /**
     * 为TextView设置字符串
     *
     * @param viewId
     * @param text
     * @return
     */
    public ListCommonViewHolder setText(int viewId, String text) {
        TextView view = getView(viewId);
        view.setText(text);
        return this;
    }

    //设置字体颜色
    public ListCommonViewHolder setTextColor(Context context, int viewId, @ColorRes int res) {
        TextView view = getView(viewId);
        view.setTextColor(ContextCompat.getColor(context, res));
        return this;
    }

    public ListCommonViewHolder setBackground(int viewId, @DrawableRes int res) {
        View view = getView(viewId);
        view.setBackgroundResource(res);
        return this;
    }

    public ListCommonViewHolder setSimpleDraweeView(int viewId, String url) {
        SimpleDraweeView simpleDraweeView = getView(viewId);
        simpleDraweeView.setImageURI(Uri.parse(url));
        return this;
    }

    public View getConvertView() {
        return mConvertView;
    }

    public ListCommonViewHolder setViewVisible(int viewId, boolean visible) {
        if (visible) {
            getView(viewId).setVisibility(View.VISIBLE);
            return this;
        }
        getView(viewId).setVisibility(View.GONE);
        return this;
    }

    public ListCommonViewHolder setImageBackground(int viewId, @DrawableRes int res) {
        ImageView view = getView(viewId);
        view.setBackgroundResource(res);
        return this;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        itemClickListener = onItemClickListener;
        mConvertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemClickListener.onItemClick(position);
            }
        });
    }
}

